package xyz.labmem.config

import cn.hutool.core.io.FileUtil
import cn.hutool.core.io.file.FileWriter
import cn.hutool.core.lang.Console
import cn.hutool.core.util.RuntimeUtil
import cn.hutool.core.util.StrUtil
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import xyz.labmem.core.exception.LabRunException
import xyz.labmem.core.util.getJarFile
import java.io.File
import java.nio.file.Files
import java.util.*
import kotlin.jvm.Throws

/**
 * @Author lty
 * @Date 2021/9/30 10:39
 */
class BrowserReptileConfig {

    companion object {

        val configPath = this::class.getJarFile().parent + "/lab_config"

        //驱动地址
        var driverDir: String? = null

        //钩子载入超时ms
        var outTime = 20000L

        //文件上传超时ms
        var uploadOutTime = 120000L

        //线程等待时间
        var threadSleep = 500L

        //显示网页图片
        var showImg = true

        //窗口最大化
        var winMax = false

        //chrome path
        var chromePath: String? = null

        init {
            readConfig()
        }

        //读取配置文件
        @Throws(LabRunException::class)
        fun readConfig() {
            try {
                val configFile = File("$configPath/app.labconf")
                if (configFile.exists()) {
                    val bytes = Files.readAllBytes(configFile.toPath())
                    val config = JSONObject.parseObject(String(bytes))
                    if (StrUtil.isNotBlank(config.getString("driverDir")))
                        driverDir = config.getString("driverDir")
                    if (config.getLong("outTime") > 0)
                        outTime = config.getLong("outTime")
                    if (config.getLong("uploadOutTime") > 0)
                        uploadOutTime = config.getLong("uploadOutTime")
                    if (config.getLong("threadSleep") > 0)
                        threadSleep = config.getLong("threadSleep")
                    if (StrUtil.isNotBlank(config.getString("chromePath")))
                        chromePath = config.getString("chromePath")
                } else {
                    write()
                }
            } catch (e: Exception) {
                e.printStackTrace()
                throw LabRunException("读取配置文件失败！", e)
            }
        }

        //创建写入配置文件
        @Throws(LabRunException::class)
        fun write() {
            try {
                val configFile = File("$configPath/app.labconf")
                if (!configFile.exists()) {
                    FileUtil.touch(configFile)
                }
                FileWriter(configFile).write(JSON.toJSONString(this))
            } catch (ex: Exception) {
                throw LabRunException("写入配置文件失败", ex)
            }
        }

        //刷新驱动
        fun refreshDriver() {
            if (StrUtil.isNotBlank(driverDir))
                refreshDriver(driverDir!!)
        }

        //刷新驱动
        fun refreshDriver(path: String) {
            if (System.getProperty("os.name").lowercase(Locale.getDefault()).indexOf("windows") >= 0) {
                System.setProperty("webdriver.chrome.driver", path)
            } else if (System.getProperty("os.name").lowercase(Locale.getDefault()).indexOf("linux") >= 0) {
                Console.log(RuntimeUtil.execForStr("sudo chmod +x $path"))
                Console.log(RuntimeUtil.execForStr("sudo cp -f $path /usr/local/share/chromedriver"))
                Console.log(RuntimeUtil.execForStr("sudo ln -sf /usr/local/share/chromedriver /usr/local/bin/chromedriver"))
                Console.log(RuntimeUtil.execForStr("sudo ln -sf /usr/local/share/chromedriver /usr/bin/chromedriver"))
                if (!File("/usr/bin/chromedriver").exists())
                    Console.error("shell Error")
            }
        }

    }

}